package com.fortune.service.impl;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.fortune.dal.dao.BankDAO;
import com.fortune.dal.model.Bank;
import com.fortune.service.BankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BankServiceImpl implements BankService {

    @Autowired
    private BankDAO bankDAO ;

    @Override
    public void addBank(Bank bank) throws BusinessException {
        try{
            bankDAO.insert(bank);
        }catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
    }

    @Override
    public void modifyBank(Bank bank) throws BusinessException {
        try{
            bankDAO.updateById(bank) ;
        }catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
    }

    @Override
    public Bank queryBank(Integer bankId) throws BusinessException {
        Bank c = null;
        try{
            c = bankDAO.queryById(bankId) ;
        }catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
        return c;
    }

    @Override
    public Bank queryBankByName(String name) throws BusinessException {
        Bank param = new Bank();
        param.setName(name);
        List<Bank> banks = queryBankList(param) ;
        if(ListUtils.isNotEmpty(banks))  {
            return banks.get(0) ;
        }
        return null;
    }



    @Override
    public List<Bank> queryBankList(Bank bank) throws BusinessException {
        List<Bank> list = null;
        try{
            list = bankDAO.queryByModel(bank) ;
        }catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
        return list;
    }
}
